﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdmLaboratorios.Business;
using AdmLaboratorios.DTO;

namespace AdmLaboratorios
{
    public partial class frmStockConsulta : Form
    {
        public frmStockConsulta()
        {
            InitializeComponent();
        }

        private void frmStockConsulta_Load(object sender, EventArgs e)
        {
            cbEstado.Items.Clear();
            cbEstado.Items.Add(eStockEstado.TODO.ToString());
            cbEstado.Items.Add(eStockEstado.APROBADO.ToString());
            cbEstado.Items.Add(eStockEstado.CUARENTENA.ToString());
            cbEstado.Items.Add(eStockEstado.RECHAZADO.ToString());
            cbEstado.SelectedIndex = 0;
        }

        private void cbEstado_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReloadStock();
        }

        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        internal void ReloadStock()
        {
            try
            {
                //gv.DataSource = null;
                gv.DataSource = Stock.Consulta(cbEstado.Text.Trim().ToUpper(), txFiltro.Text.Trim());
                gv.Refresh();
                lblRegistros.Text = ((DataTable)gv.DataSource).Rows.Count.ToString();
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }
        }

        private void txFiltro_TextChanged(object sender, EventArgs e)
        {
            ReloadStock();
        }

        private void btnAjuste_Click(object sender, EventArgs e)
        {
            Ajuste();
        }

        private void Ajuste()
        {
            try
            {
                if (gv.SelectedRows.Count == 1)
                {
                    using (frmStockAjuste f = new frmStockAjuste())
                    {
                        f.LoadStock(Stock.Get((int)gv.SelectedRows[0].Cells["Id"].Value));
                        FormsHelper.CargarColumnasGrilla(f);
                        if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            ReloadStock();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                FormsHelper.MsgErrorInesperado(ex);
            }
        }

        private void gv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Ajuste();
        }
    }
}
